export const time = {
	//Y/M/d或者Y-M-d格式的时间转时间戳
	timeToTimestamp: (time)=>{
		let reg = new RegExp('-','g')
		var newtime = time.replace(reg,'/')
		let t = new Date(newtime)
		let t_s = t.getTime()
		return t_s
	},
	// Y-M-d H:M:S格式的时间转时间戳
	detailTimeToTimestamp: (time)=>{
		var thisTime = time;
		thisTime = thisTime.replace(/-/g, '/');
		var time = new Date(thisTime);
		time = time.getTime();
		return time
	},
	
	//时间戳转日期
	timestampToDate: (timestamp)=>{
		let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
		let m = date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1
		let d = date.getDate() < 10 ? '0'+date.getDate() : date.getDate()
		return m+'月'+d+'日'
	},
	//时间戳转日期 -
	timestampToDate_ymd: (timestamp)=>{
		let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
		let y = date.getFullYear();
		let m = date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1
		let d = date.getDate() < 10 ? '0'+date.getDate() : date.getDate()
		return y+'-'+m+'-'+d
	},
	//时间戳转日期 格式20200908
	timestampToDate_Num: (timestamp)=>{
		let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
		let y = date.getFullYear();
		let m = date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1
		let d = date.getDate() < 10 ? '0'+date.getDate() : date.getDate()
		return y.toString()+m.toString()+d.toString()
	},
	//时间戳转时分
	timestampToHour: (timestamp)=>{
		let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
		let h = date.getHours() < 10 ? '0'+date.getHours() : date.getHours()
		let m = date.getMinutes() < 10 ? '0'+ date.getMinutes() : date.getMinutes()
		return h+':'+m
	},
	//时间戳转时分秒
	timestampToHMS: (timestamp)=>{
		let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
		let h = date.getHours() < 10 ? '0'+date.getHours() : date.getHours()
		let m = date.getMinutes() < 10 ? '0'+ date.getMinutes() : date.getMinutes()
		let s = date.getSeconds() < 10 ? '0'+ date.getSeconds() : date.getSeconds()
		return h+':'+m+':'+s
	},
	//时间戳转日时分秒
	timestampToYMDHMS: (timestamp)=>{
		let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
		let y = date.getFullYear();
		let m = date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1
		let d = date.getDate() < 10 ? '0'+date.getDate() : date.getDate()
		let h = date.getHours() < 10 ? '0'+date.getHours() : date.getHours()
		let mm = date.getMinutes() < 10 ? '0'+ date.getMinutes() : date.getMinutes()
		let s = date.getSeconds() < 10 ? '0'+ date.getSeconds() : date.getSeconds()
		return y+'年'+m+'月'+d+'日'+'  '+h+':'+mm+':'+s
	},
	//时间戳转日时分秒
	timestampToYMDHM: (timestamp)=>{
		let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
		let y = date.getFullYear();
		let m = date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1
		let d = date.getDate() < 10 ? '0'+date.getDate() : date.getDate()
		let h = date.getHours() < 10 ? '0'+date.getHours() : date.getHours()
		let mm = date.getMinutes() < 10 ? '0'+ date.getMinutes() : date.getMinutes()
		return y+'-'+m+'-'+d+' '+h+':'+mm
	},
	//时间戳转周几
	timestampToWeek: (timestamp)=>{
		let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
		let week = "周" + "日一二三四五六".charAt(date.getDay());
		return week
	},
	//获取某时间n分钟之前的时间戳
	getTimestampBeforeMinute:(date, n)=>{
		date.setMinutes(date.getMinutes()-n)
		return date.getTime()
	},
	//获取某时间n分钟之后的时间戳
	getTimestampAfterMinute:(date,n)=>{
		date.setMinutes(date.getMinutes()+n)
		return date.getTime()
	},
	//获取某时间n天之后的时间戳
	getTimestampAfterDay:(date,n)=>{
		date.setDate(date.getDate()+n)
		return date.getTime()
	},
	//获取当前时间的时间戳
	getNowTimestamp(){
		let date = new Date()
		return date.getTime()
	},
	getCurrentDateTime() {
		var date = new Date();
		let y = date.getFullYear();
		let M = date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1
		let d = date.getDate() < 10 ? '0'+date.getDate() : date.getDate()
		let h = date.getHours() < 10 ? '0'+date.getHours() : date.getHours()
		let m = date.getMinutes() < 10 ? '0'+ date.getMinutes() : date.getMinutes()
		let s = date.getSeconds() < 10 ? '0'+ date.getSeconds() : date.getSeconds()
		return y+'-'+M+'-'+d+' '+h+':'+m+':'+s
	},
	getCurrentDate(){
		var date = new Date();
		let y = date.getFullYear();
		let M = date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1
		let d = date.getDate() < 10 ? '0'+date.getDate() : date.getDate()
		return y+'-'+M+'-'+d
	},
	//聊天窗口格式化时间
	formatChatTime(time) {
		time = this.dateFormat("YY-mm-dd HH:MM:SS", new Date(time));
		var date = time.toString();
		var year = date.split("-")[0];
		var month = date.split("-")[1];
		var day = date.split("-")[2];
		var d1 = new Date(year + '/' + month + '/' + day.split(" ")[0]);
		var d3 = new Date(date.replace(/-/g, "/"));
		var dd = new Date();
		var y = dd.getFullYear();
		var m = dd.getMonth() + 1;
		var d = dd.getDate();
		var d2 = new Date(y + '/' + m + '/' + d);
		var iday = parseInt(d2 - d1) / 1000 / 60 / 60 / 24;
		var hours = d3.getHours();
		var minutes = d3.getMinutes();
		if (minutes < 10) {
			minutes = '0' + minutes;
		}
		if (hours < 10) {
			hours = '0' + hours;
		}
		if (iday == 0) {
			if (hours >= 12) {
				return "下午 " + hours + ":" + minutes;
			} else {
				return "上午 " + hours + ":" + minutes;;
			}
		} else if (iday == 1) {
			var dt = "";
			if (hours >= 12) {
				dt = "下午 " + hours + ":" + minutes;
			} else {
				dt = "上午 " + hours + ":" + minutes;;
			}
			return "昨天 " + dt;
		} else if (iday == 2) {
			var dt = "";
			if (hours >= 12) {
				dt = "下午 " + hours + ":" + minutes;
			} else {
				dt = "上午 " + hours + ":" + minutes;;
			}
			return "前天 " + dt;
		} else {
			return year + '/' + month + "/" + d1.getDate()
		}
	},
	//自定义格式化时间
	dateFormat(fmt, time) {
		 var date = new Date(time)
		let ret;
		const opt = {
			"Y+": date.getFullYear().toString(), // 年
			"m+": (date.getMonth() + 1).toString(), // 月
			"d+": date.getDate().toString(), // 日
			"H+": date.getHours().toString(), // 时
			"M+": date.getMinutes().toString(), // 分
			"S+": date.getSeconds().toString() // 秒
		};
		for (let k in opt) {
			ret = new RegExp("(" + k + ")").exec(fmt);
			if (ret) {
				fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
			};
		};
		return fmt;
	},
}

